package com.itcast.manager3.B_controller;

import com.itcast.manager2.E_domain.Student;
import com.itcast.manager3.C_service.TecherService;
import com.itcast.manager3.tomian.TecherTomain;

import java.util.ArrayList;
import java.util.Scanner;

public class TecherContorller {
    Scanner sc = new Scanner(System.in);
    TecherService ts = new TecherService();

    public void start() {
        while (true) {
            System.out.println("---欢迎进入老师管理系统---");
            System.out.println("1:添加老师");
            System.out.println("2:删除老师");
            System.out.println("3:修改老师");
            System.out.println("4:查看老师");
            System.out.println("5:退出");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    addTecher();
                    break;
                case 2:
                    deleteTecher();
                    break;
                case 3:
                    updeteTecher();
                    break;
                case 4:
                    findAllTecher();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void updeteTecher() {
        System.out.println("请输入要修改的老师工号");
        String id = sc.next();
        if (getTecherRepetition(id)==false){
            System.out.println("没有该老师的信息，请重新输入");
            return;
        }
        System.out.println("请输入要修改的老师名字");
        String name = sc.next();
        System.out.println("请输入要修改的老师年龄");
        int age = sc.nextInt();
        System.out.println("请输入要修改的老师生日");
        String birthday = sc.next();
        TecherTomain tt  = new TecherTomain(id,name,age,birthday);
        ts.updeteTcher(tt);
        System.out.println("修改成功");
    }

    private void deleteTecher() {
        System.out.println("请输入要删除的老师信息");
        String id = sc.next();
        if (getTecherRepetition(id)==false){
            System.out.println("没有该老师的信息，请重新输入");
            return;
        }
        ts.deleteTecher(id);
        System.out.println("删除成功");
    }

    private void findAllTecher() {
        ArrayList<TecherTomain> list = ts.getAllTecher();
        if (list.size() == 0) {
            System.out.println("老师管理系统没有录入老师信息，请录入后查看");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            TecherTomain tt = list.get(i);
            System.out.println(tt.getId() + "--" + tt.getName() + "--" + tt.getAge() + "--" + tt.getBirthday());
        }
    }

    private void addTecher() {
        System.out.println("请输入老师工号");
        String id = sc.next();
        if (getTecherRepetition(id) == true) {
            System.out.println("已有输入工号，请重新输入");
            return;
        }
        System.out.println("请输入老师名字");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        int age = sc.nextInt();
        System.out.println("请输入老师生日");
        String birthday = sc.next();
        TecherTomain th = new TecherTomain(id, name, age, birthday);
        ts.addTecher(th);
    }

    public boolean getTecherRepetition(String id) {
        boolean flag = ts.isExistss(id);
        if (flag == true) {
            return true;
        }
        return false;
    }
}
